package org.xjo.main;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class GOLDUpdater {

	//public static String  FILE_PATH ="C:\\historicaldata\\xjo\\audusd.csv";

	public static String  FILE_PATH ="C:\\historicaldata\\xjo\\gold.csv";

	//public static String  FILE_PATH ="xjo.csv";

	public static String strURL="http://www.investing.com/commodities/gold-historical-data";



public static void main(String[] args) {


updateXJO();

}

public static void updateXJO(){


	//updateFile("ne conent");
	  BufferedReader bReader=null;

	  String strCurrentLine=null;



	  try{
		  URL url = new URL(strURL);

		    HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
		    urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (compatible; MSIE 6.0; Windows NT 5.1)");

		    String contentType = urlConn.getContentType();

		  //  System.out.println("contentType:" + contentType);

		    InputStream is = urlConn.getInputStream();


		  bReader= new BufferedReader(new InputStreamReader(is));

		  String strMadeLine="";

		  String strContent="";

		  String firstLine=findFirstLine();


	if(firstLine!=null&&firstLine.length()>0){
		  while((strCurrentLine=bReader.readLine())!=null){
			 // strCurrentLine="";
			  strMadeLine="";
			  if(strCurrentLine.indexOf("first left bold noWrap")>0){
				  System.out.println(strCurrentLine);
				  strMadeLine+="\""+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"))+"\"";
				  if(firstLine.indexOf(strMadeLine)>=0){
					break ;
				  }
				  strCurrentLine=bReader.readLine();
				  strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"));
			     System.out.println(strCurrentLine);
			      strCurrentLine=bReader.readLine();
			      strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"));
			      System.out.println(strCurrentLine);
			      strCurrentLine=bReader.readLine();
			      strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"));
			      System.out.println(strCurrentLine);
			      strCurrentLine=bReader.readLine();
			      strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"));

			      strCurrentLine=bReader.readLine();
			      System.out.println(strCurrentLine);
			      strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf('>')+1,strCurrentLine.indexOf("</"));

			      System.out.println(strMadeLine);

			     /* strMadeLine+=","+strCurrentLine.substring(strCurrentLine.indexOf("\">",strCurrentLine.indexOf('>')+1)+2,strCurrentLine.indexOf("</",strCurrentLine.indexOf("</")+1));




			      strCurrentLine=bReader.readLine();*/
			     // System.out.println(strCurrentLine);

			      System.out.println("strMadeLine-"+ strMadeLine +"\r\n");

			      strContent+=strMadeLine+"\r\n";
				  }


			}

	}

	if(strContent!=null){

		  updateFile(strContent);

	}




	  }catch (Exception e) {
		// TODO: handle exception
		  e.printStackTrace();
	}
	  finally{
		  try{
		  bReader.close();
		  }catch (Exception e) {
			// TODO: handle exception
		}
	    }


}

private static String findFirstLine(){

	BufferedReader bReader=null;
	String strFirstLine=null;

	try{
		bReader=new BufferedReader(new FileReader(FILE_PATH));

		if((strFirstLine=bReader.readLine())!=null){




		}


	}catch (Exception e) {
		// TODO: handle exception
	}finally{
		try{
			bReader.close();
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

	return strFirstLine;

}


	public static void updateFile(String strNewContent){

  String strFirstLine=null;

  StringBuffer currentContent=new StringBuffer();

  BufferedReader bReader=null;

  BufferedWriter bWriter=null;

		try{
		//FileReader fr=new FileReader(FILE_PATH);

			bReader=new BufferedReader(new FileReader(FILE_PATH));

			//currentContent.append("susuus \r\n");

			currentContent.append(strNewContent);

			while((strFirstLine=bReader.readLine())!=null){


				currentContent.append(strFirstLine+"\r\n");

			}



		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{

			try{
				bReader.close();


			}catch (Exception e) {
				// TODO: handle exception
			}
		}


		try{
		bWriter=new BufferedWriter(new FileWriter(FILE_PATH));

		bWriter.write(currentContent.toString());
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		finally{
			try{
			bWriter.close();
			}catch (Exception e) {
				// TODO: handle exception
			}
		}


	}


}
